package com.lhn.netty.netty_demo.inboundhandlerandoutboudhhandler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author lhn
 * @date 2025/06/11 19:42
 * @since 1.0.0
 **/
public class MyLongToByteEncoder extends MessageToByteEncoder<Long> {

    /**
     *
     * @param channelHandlerContext 上下文
     * @param aLong 待编码的Long
     * @param byteBuf 待写入的ByteBuf
     * @throws Exception 抛出异常
     */
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Long aLong, ByteBuf byteBuf) throws Exception {
        System.out.println("MyLongToByteEncoder encode 被调用");
        System.out.println("msg = " + aLong);
        // 添加编码逻辑 因为8个字节才能转换成一个long
        byteBuf.writeLong(aLong);
        System.out.println("8个字节已写入ByteBuf");
    }
}